// pages/ActivityDetail/ActivityDetail.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active_index: 1,
    huodongDetail: '',
    baoming: true,
    daanshow: 1,
    baoList: [],
    support: false
  },

  toEnrollList: function (e) {
    // 指定展示状态: attending, checkIn
    wx.setStorageSync('listStatus', e.currentTarget.dataset.listStatus)
    wx.navigateTo({
      url: '/pages/EnrollList/EnrollList',
    })
  },

  tabItemShow: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.id;
    that.setData({
      active_index: index
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var taskId = wx.getStorageSync("taskId")
    var data = {
      paraUrl: "task/find?taskId=" + taskId,
      paraData: ''
    }
    app.getRequest(data, function (res) {
      if (res.data.result == 200) {
        that.setData({
          huodongDetail: res.data.body
        })
        var data1 = {
          paraUrl: "interaction/create",
          paraData: {
            "type": "browse", // 交互动作类型
            "interactionTarget": "task",
            "target": { // 具体是什么交互对象, 决定了里面的值是什么
              "id": res.data.body.id,
              "code": res.data.body.code,
              "name": res.data.body.name
            }
          }
        }
        app.postRequest(data1, function (res) {

        })
        // 是否点赞supportsupport:false
        if (that.data.huodongDetail.support) {
          that.setData({
            support: true
          })
        }
        // 是否加入
        if (that.data.huodongDetail.status.name == '已结束') {
          that.setData({
            daanshow: 4
          })
          return;
        }
        if (res.data.body.attend) {
          if (res.data.body.status.name != "未开始") {
            // 是否签到
            if (res.data.body.checkIn) {
              that.setData({
                daanshow: 2
              })
            } else {
              that.setData({
                daanshow: 1
              })
            }
          } else {
            that.setData({
              daanshow: 5
            })
          }
        } else {
          that.setData({
            daanshow: 3
          })
        }
      } else {
      }
    })
    var taskId = wx.getStorageSync("taskId")
    var data2 = {
      paraUrl: "attendance/list/task?taskId=" + taskId,
      paraData: ''
    }
    app.getRequest(data2, function (res) {
      if (res.data.result == 200) {
        that.setData({
          baoList: res.data.body
        })
      } else {

      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  woyaobaoming: function () {
    var that = this;
    that.setData({
      baoming: false
    })
  },
  confirm: function () {
    var that = this;
    var taskId = wx.getStorageSync("taskId")
    var data = {
      paraUrl: "attendance/attend?taskId=" + taskId,
      paraData: ''
    }
    app.postRequest(data, function (res) {
      if (res.data.result == 200) {
        wx.showToast({
          title: '报名成功',
          icon: 'success',
          duration: 2000
        })
        that.onShow();
      } else if (res.data.result == 60002) {
        wx.showToast({
          title: '请勿重复报名',
          icon: 'success',
          image: '/images/shibai.png',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: '报名失败',
          icon: 'success',
          image: '/images/shibai.png',
          duration: 2000
        })
      }
      that.setData({
        baoming: true
      })
    })
  },
  cancel: function () {
    var that = this;
    that.setData({
      baoming: true
    })
  },
  qiandao: function () {
    var that = this;
    var taskId = wx.getStorageSync("taskId")
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res)
        var latitude = res.latitude
        var longitude = res.longitude
        var data = {
          paraUrl: "attendance/check-in?taskId=" + taskId + '&latitude=' + latitude + '&longitude=' + longitude,
          paraData: ''
        }
        app.postRequest(data, function (res) {
          if (res.data.result == 200) {
            wx.showToast({
              title: '签到成功',
              icon: 'success',
              duration: 2000
            })
            that.onShow();
          } else if (res.data.result == 60007) {
            wx.showToast({
              title: '请勿重复签到',
              icon: 'success',
              image: '/images/shibai.png',
              duration: 2000
            })
          } else if (res.data.result == 60014) {
            wx.showToast({
              title: '不在签到范围内',
              icon: 'success',
              image: '/images/shibai.png',
              duration: 2000
            })
          } else {
            wx.showToast({
              title: '签到失败',
              icon: 'success',
              image: '/images/shibai.png',
              duration: 2000
            })
          }
          that.setData({
            baoming: true
          })
        })
      }
    })



  },
  dianzan: function (e) {
    var that = this;
    var data = {
      paraUrl: 'interaction/create',
      paraData: {
        "type": "support",
        "interactionTarget": "task",
        "target": {
          "id": that.data.huodongDetail.id,
          "code": that.data.huodongDetail.code,
          "name": that.data.huodongDetail.name
        }
      }
    }
    app.postRequest(data, function (res) {
      if (res.data.result == 200) {
        wx.showToast({
          title: '点赞成功',
          icon: 'success',
          duration: 2000
        })
        // 成功则刷新页面, 间接禁用按钮和更新数字
        that.onShow();
      } else if (res.data.result == 60013) {
        wx.showToast({
          title: '不能重复点赞',
          icon: 'success',
          image: '/images/shibai.png',
          duration: 2000
        })
      }
    })
  },
  // 发起交互
  interaction: function (e) {
    var that = this;
    var data = {
      paraUrl: 'interaction/create',
      paraData: {
        "type": e.currentTarget.dataset.actionType,
        "interactionTarget": "task",
        "target": {
          "id": that.data.huodongDetail.id,
          "code": that.data.huodongDetail.code,
          "name": that.data.huodongDetail.name
        }
      }
    }
    app.postRequest(data, function (res) {

    })
  },
  ditutiao() {
    var that = this;
    wx.setStorageSync('latitude', that.data.huodongDetail.latitude)
    wx.setStorageSync('longitude', that.data.huodongDetail.longitude)
    wx.navigateTo({
      url: '/pages/map/map',
    })
  }
})